var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

// routers
// 要配置新的路由就按照下列规则去配置
// var indexRouter = require('./router/index');
var usersRouter = require('./router/user');


var app = express();

// 启动 模板引擎
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

app.use(logger('dev')); //dev 日志
app.use(express.json());    //json转换工具
app.use(express.urlencoded({ extended: false }));   //解析请求头
app.use(cookieParser());                            //解析cookie
app.use(express.static(path.join(__dirname, 'public')));

// app.use('/', indexRouter);
app.use('/users', usersRouter);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
	next(createError(404));
});

// 错误处理 默认跳转 500页面
app.use(function(err, req, res, next) {
	// set locals, only providing error in development
	res.locals.message = err.message;
	res.locals.error = req.app.get('env') === 'development' ? err : {};
	
	// render the error page
	res.status(err.status || 500);
	res.render('error');
});

/*app.listen(3000,()=>{
	console.log("http://127.0.0.1:3000")
})*/
module.exports = app;

